unit Func;

interface

uses
  Data.SqlExpr, System.Classes, Datasnap.DBClient, Vcl.DBGrids, GridLista,
  Vcl.Buttons;

type
  TFunc = class(TObject)
  private

  protected

  public
    function query(value: string): TSqlQuery;
    function cdsQuery(value: string): TClientDataSet;
    procedure execute(strSql: string);
    procedure clearFields(Form: TComponent);
    procedure addColunas(Grid: TGridLista; Index, Largura: Integer;
      Campo, Titulo: String; Visivel: Integer = 1);
    procedure addButtonGrid(Grid: TGridLista; Button: TSpeedButton;
      Coluna: Integer);
  end;

implementation

uses
  DalDM, Data.DBXCommon, System.SysUtils, Vcl.StdCtrls, Vcl.Mask, System.Types;

{ TFunc }

procedure TFunc.addButtonGrid(Grid: TGridLista; Button: TSpeedButton;
  Coluna: Integer);
Var
  DataRect: TRect;
begin
  // Place the button in the first column.
  If (Grid.Columns[Coluna].Index = 3) Then
  Begin
    With Grid Do
    Begin
      DataRect := CellRect(Grid.Columns[Coluna].Index, Row);
    End;
    // Assign the button's parent to the grid.
    If (Button.Parent <> Grid) Then
    begin
      Button.Parent := Grid;
    end;
    // Set the button's coordinates.
    // In this case, right justify the button.
    If (Button.Left <> (DataRect.Right - Button.Width)) Then
    begin
      Button.Left := (DataRect.Right - Button.Width);
    end;
    If (Button.Top <> DataRect.Top) Then
    begin
      Button.Top := DataRect.Top;
    end;
    // Make sure the button's height fits in row.
    If (Button.Height <> (DataRect.Bottom - DataRect.Top)) Then
    begin
      Button.Height := (DataRect.Bottom - DataRect.Top);
    end;
  End
end;

procedure TFunc.addColunas(Grid: TGridLista; Index, Largura: Integer;
  Campo, Titulo: String; Visivel: Integer = 1);
begin
  Grid.Columns.Add;
  Grid.Columns[Index].FieldName := Campo;
  Grid.Columns[Index].Title.Caption := Titulo;
  Grid.Columns[Index].Width := Largura;
  if (Visivel <> 1) then
    Grid.Columns[Index].Visible := false;
end;

function TFunc.cdsQuery(value: string): TClientDataSet;
begin
  Result := nil;
  dm.cdsQuery.Close;
  dm.cdsQuery.CommandText := value;
  try
    if (UpperCase(Copy(value, 1, 6)) = 'SELECT') then
    begin
      dm.cdsQuery.Open;
      Result := dm.cdsQuery;
    end
    else
    begin
      dm.cdsQuery.execute;
    end;
  except
    Abort;
  end;
end;

procedure TFunc.clearFields(Form: TComponent);
var
  i: Integer;
begin
  for i := 0 to Form.ComponentCount - 1 do
  begin
    if (Form.Components[i].ClassName = 'TEdit') then
    begin
      (Form.Components[i] as TEdit).Clear;
    end;
    if (Form.Components[i].ClassName = 'TMaskEdit') then
    begin
      (Form.Components[i] as TMaskEdit).Clear;
    end;
    if (Form.Components[i].ClassName = 'TMemo') then
    begin
      (Form.Components[i] as TMemo).Clear;
    end;
  end;
end;

procedure TFunc.execute(strSql: string);
var
  tr: TDBXTransaction;
begin
  tr := dm.sqlConnect.BeginTransaction(TDBXIsolations.ReadCommitted);
  try
    with dm.sqlQuery do
    begin
      sql.Clear;
      sql.Add(strSql);
      ExecSQL();
    end;
    dm.sqlConnect.CommitFreeAndNil(tr);
  except
    on E: Exception do
      dm.sqlConnect.RollbackIncompleteFreeAndNil(tr);
  end;
end;

function TFunc.query(value: string): TSqlQuery;
begin
  with dm.sqlQuery do
  begin
    Close;
    sql.Clear;
    sql.Add(value);
    Open;
  end;
  Result := dm.sqlQuery;
end;

end.
